<template>
  <div class="loginContent">
    <div class="loginArea">
      <div class="loginLogo">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单登录区域 -->
      <el-form
        label-width="0px"
        class="loginForm"
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
      >
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" placeholder="账号" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="密码"
            type="password"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button type="info" @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 14, message: '长度在 3 到 14 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    let _self = this
    document.onkeydown = function(e) {
      let key = window.event.keyCode
      if (key === 13) {
        _self.submitForm('loginForm')
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return

        this.$http
          .post('/apis/login', {
            username: this.loginForm.username,
            pwd: this.loginForm.password
          })
          .then(res => {
            // console.log(res.data)
            if (res.data.code === 0) {
              window.sessionStorage.setItem('token', res.data.data.token)
              window.sessionStorage.setItem('username', res.data.data.username)
              this.$router.push({ path: '/home' })
            } else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              })
            }
          })
          .catch(err => {
            console.log('err', err)
          })
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.loginContent {
  background: #2b4b6b;
  width: 100%;
  height: 100%;
}

.loginArea {
  width: 450px;
  height: 300px;
  background: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;
  .loginLogo {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.loginForm {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
